package 动态规划;

import org.junit.Test;

public class Solution213 {
    public int rob(int[] nums) {
        if (nums == null || nums.length == 0) {
            return 0;
        }
        if (nums.length == 1) {
            return nums[0];
        }
        if (nums.length == 2) {
            return Math.max(nums[0], nums[1]);
        }
        int[] dp = new int[nums.length];
        int[] dp1 = new int[nums.length];
        dp[0] = nums[0];
        dp[1] = Math.max(nums[1], nums[0]);
        for (int i = 2; i < nums.length - 1; i++) {
            /*可与选在上一个或者本个+往上两个*/
            dp[i] = Math.max(dp[i - 1], nums[i] + dp[i - 2]);
        }
        dp1[1] = nums[1];
        dp1[2] = Math.max(nums[1], nums[2]);
        for (int i = 3; i < nums.length; i++) {
            /*可与选在上一个或者本个+往上两个*/
            dp1[i] = Math.max(dp1[i - 1], nums[i] + dp1[i - 2]);
        }

        return Math.max(dp[nums.length - 2], dp1[nums.length - 1]);
    }

    public int rob2(int[] nums) {
        int[] dp1 = new int[nums.length + 1];
        int[] dp2 = new int[nums.length + 1];
        dp1[0] = nums[0];
        dp1[1] = Math.max(nums[1], nums[0]);
        for (int i = 3; i < nums.length; i++) {
            dp1[i] = Math.max(nums[i - 1] + dp1[i - 2], dp1[i - 1]);
        }
        dp2[1] = nums[1];
        dp2[2] = Math.max(nums[1], nums[2]);
        for (int i = 4; i <= nums.length; i++) {
            dp2[i] = Math.max(nums[i - 1] + dp2[i - 2], dp2[i - 1]);
        }
        return Math.max(dp1[nums.length - 1], dp2[nums.length]);
    }


    @Test
    public void test(){
        int[] nums=new int[]{1, 2, 3, 4, 5, 1, 2, 3, 4, 5};
        rob(nums);
    }
}
